<?php
/**
 *
 * @author andrew carlson <andrew@acstudiosllc.com>
 * @version 0.0.1
 * @copyright 2012 ACStudios LLC
 * @package default
 */

namespace Encompass\Projects;

/**
 * Projects controller
 *
 * @package default
 * @author  andrew carlson <andrew@acstudiosllc.com>
 */
class projectsView {
	
	function __construct(){
		$loader = new \Twig_Loader_Filesystem('templates/');

		if (CACHING) {
			$twig = new \Twig_Environment($loader, array('cache' => 'tmp/cache'));
		} else {
			$twig = new \Twig_Environment($loader);
		}
	}
	
	function display() {
		$loader = new \Twig_Loader_Filesystem('templates/');

		if (CACHING) {
			$twig = new \Twig_Environment($loader, array('cache' => 'tmp/cache'));
		} else {
			$twig = new \Twig_Environment($loader);
		}

		$template = $twig -> loadTemplate('projects.tpl');

		$template -> display(array('pageTitle' => 'Encompass Projects'));
	}
	
	function dhtmlxJson($projects) {
		foreach ($projects as &$row) {
			$dhtmlxData[] = array(
				'id' => $row['story_id'], 
				'data' => array(
					$row['priority'], 
					$row['type'], 
					$row['description'],
					($row['estimate'] / 60), 
					$row['state']
				), 
			);
		}

		$dhtmlResults = array(
			'total_count' => count($projects), 
			'rows' => $dhtmlxData
		);
		
		echo json_encode($dhtmlResults);
	}
	
	/**
	 * format data to dhtmlxGantt XML
	 */
	function convertToXml($data) {
		$projectsXML =  new \SimpleXMLElement("<projects></projects>");

		// this works, but need to break projects and tasks up more
		// and be able to handle switching from one array to another to 
		// build out the necessary data
		foreach ($data as $key => &$project) {
			$proj = $projectsXML->addChild('project');
			$proj->addAttribute('id', $project['story_id']);
			$proj->addAttribute('name', $project['description']);
			$proj->addAttribute('startdate', str_replace("-", ",", $project['started_on']));
			$task = $proj->addChild('task');	// for some reason this breaks dhtmlxGantt
			$task->addAttribute('id', 1);
			$task1 = $task->AddChild('name', 'A Test Task');
			$task1 = $task->AddChild('est', $project['started_on']);
			$task1 = $task->AddChild('duration', '25');
			$task1 = $task->AddChild('percentcompleted', '15');
			$task1 = $task->AddChild('predecessortasks');
			$task1 = $task->AddChild('childtasks');
		}

		//Header('Content-type: text/xml');	
		echo $projectsXML->asXML();
	}

}
?>
